package com.lefevre.cms.web.action.statistic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.statistic.PV;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.statistic.PageViewService;
import com.lefevre.cms.utils.IpAddress;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.utils.Verification;
import org.queryString.util.UrlEncoded;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 页面浏览量
 *
 * @author Lefevre
 */
@Controller
public class PageViewAction {

    @Resource
    PageViewService pageViewService;
    @Resource
    SettingService settingService;

    /**
     * 页面浏览量列表
     */
    @ResponseBody
    @RequestMapping("/control/pageView/list")
    public String execute(PageForm pageForm, String start_times, String end_times) throws Exception {

        Map<String, String> error = new HashMap<>();
        Date _start_times = null;
        Date _end_times = null;
        if (start_times != null && !"".equals(start_times.trim())) {
            boolean start_createDateVerification = Verification.isTime_minute(start_times.trim());
            if (start_createDateVerification) {
                DateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                _start_times = dd.parse(start_times.trim());
            } else {
                error.put("start_times", "请填写正确的日期");
            }
        }
        if (end_times != null && !"".equals(end_times.trim())) {
            boolean end_createDateVerification = Verification.isTime_minute(end_times.trim());
            if (end_createDateVerification) {
                DateFormat dd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                _end_times = dd.parse(end_times.trim());
            } else {
                error.put("end_times", "请填写正确的日期");
            }
        }
        //比较时间
        Calendar start = Calendar.getInstance();//时间 起始
        Calendar end = Calendar.getInstance();//时间 结束
        if (_start_times != null) {
            start.setTime(_start_times);
        }
        if (_end_times != null) {
            end.setTime(_end_times);
        }
        if (_start_times != null && _end_times != null) {
            int result = start.compareTo(end);//起始时间与结束时间比较
            if (result > 0) {//起始时间比结束时间大
                error.put("start_times", "起始时间不能比结束时间大");
            }
        }
        LambdaQueryWrapper<PV> wrapper = new LambdaQueryWrapper<>();
        //起始时间
        if (_start_times != null) {
            wrapper.ge(PV::getTimes, _start_times);
        }
        //结束时间
        if (_end_times != null) {
            wrapper.le(PV::getTimes, _end_times);
        }
        wrapper.orderByDesc(PV::getTimes);
        PageView<PV> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10);
        int firstindex = (pageForm.getPage() - 1) * pageView.getMaxresult();

        QueryResult<PV> qr = new QueryResult<>();
        Page<PV> page = pageViewService.page(new Page<>(firstindex, pageView.getMaxresult()), wrapper);

        if (!ObjectUtils.isEmpty(page.getRecords())) {
            for (PV pv : page.getRecords()) {
                if (!StrUtil.isBlank(pv.getIp())) {
                    pv.setIpAddress(IpAddress.queryAddress(pv.getIp()));
                }
                if (!StrUtil.isBlank(pv.getUrl())) {
                    pv.setUrl(UrlEncoded.decodeString(pv.getUrl().trim()));
                }
                if (!StrUtil.isBlank(pv.getReferrer())) {
                    pv.setReferrer(UrlEncoded.decodeString(pv.getReferrer().trim()));
                }
            }
        }

        qr.setResultlist(page.getRecords());
        qr.setTotalrecord(page.getTotal());
        pageView.setQueryResult(qr);


        if (error.size() == 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, pageView));
        }
        return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
    }
}
